﻿Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Xml
Imports System
Imports System.Xml.XPath
Imports System.Math

Public Class Form1
    Dim CheckedList As String = "" '定义全局字符变量，用于传入 获取到的CheckedListBox1被选中的值给SQL语句
    Dim myDataset As New DataSet
    'Dim strSql As String
    Dim dt1 As New DataTable

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim tex As String = ""
        For Each DataRowView In CheckedListBox1.CheckedItems
            tex = tex & "," & "'" & DataRowView("DICT_INFO").ToString() & "'"
        Next
        CheckedList = tex
        MsgBox(CheckedList)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        'CheckedListBox1.Visible = False
        Dim CnnStr As String = "server=.;database=YZTF_PRMS_0211;user id=sa;password=sa"
        'Dim CnnStr As String = "server=.;database=YYTF_PRMS_0202;user id=sa;password=sa"
        '定义CnnStr为字符，把所要联接的SQL数据库信息赋予给它；
        Dim conn As SqlClient.SqlConnection = New SqlConnection(CnnStr)
        Dim strSql As String
        'Dim strSql1 As String
        Dim DataAdapter As New SqlDataAdapter
        'Dim DataAdapter1 As New SqlDataAdapter
        Dim dst As New DataSet
        'Dim dst1 As New DataSet
        Dim dt As New DataTable

        CheckedListBox1.CheckOnClick = True

        conn.Open()
        strSql = "SELECT DICT_CODE, DICT_INFO FROM TBL_DICT"
        Dim myCmd As SqlCommand
        myCmd = New SqlCommand(strSql, conn)
        DataAdapter.SelectCommand = myCmd
        DataAdapter.Fill(dst, "info")
        dt = dst.Tables("info")

        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "DICT_INFO"
        ComboBox1.ValueMember = "DICT_CODE"

        CheckedListBox1.DataSource = dt
        CheckedListBox1.DisplayMember = "DICT_INFO"
        CheckedListBox1.ValueMember = "DICT_CODE"

        Dim myreader = myCmd.ExecuteReader
        Dim i As Integer
        For i = 1 To 12
            System.Console.WriteLine(i)
            While myreader.Read
                System.Console.WriteLine(myreader.GetValue(1))
            End While
        Next

        Dim datarow As DataRow

        For i = 1 To 12
            System.Console.WriteLine(i)
            System.Console.WriteLine("----------------")
            For Each datarow In dt.Rows
                System.Console.WriteLine(datarow("DICT_INFO"))
            Next
        Next i

        conn.Close()
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            Dim i As Integer
            For i = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(i, True)


            Next i
        Else
            Dim i As Integer
            For i = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(i, False)
            Next i
        End If

        CheckedListBox1_SelectedIndexChanged(CheckedListBox1, e)
    End Sub

    Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
        Dim tex As String = ""
        For Each DataRowView In CheckedListBox1.CheckedItems
            tex = tex & "," & "'" & DataRowView("DICT_INFO").ToString() & "'"
        Next
        CheckedList = tex
        'MsgBox(CheckedList)
    End Sub

    Private Sub ComboBox1_DropDownClosed(sender As Object, e As EventArgs)
        MsgBox("CLOSE")
        CheckedListBox1.Visible = False

    End Sub

    Private Sub ComboBox1_DropDown(sender As Object, e As EventArgs)
        CheckedListBox1.Visible = True
    End Sub

    Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs)

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        '
    End Sub
End Class
